package com.baicaoyuan.web.common.annotation;

import com.baicaoyuan.web.common.LogUtil;
import com.baicaoyuan.web.common.exception.StateMessage;
import com.baicaoyuan.web.common.exception.StatusException;

import javax.validation.constraints.NotNull;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 功能描述：
 *
 * @author 罗富祥
 * 创建时间： 2018/8/17
 * 版权：百草园电子商务
 */
public interface IValidate {

    /**
     * 根据注解判断值
     *
     * @throws StatusException
     */
    default void fieldValidate() throws StatusException {
        Field[] fields = this.getClass().getDeclaredFields();
        String allmsg = "     ";
        for (Field field : fields) {
            String fieldName = field.getName();
            Object fieldValue = runGetter(field, this);

            boolean isAnnotationNotNull = field.isAnnotationPresent(NotNull.class);
            if (isAnnotationNotNull) {
                if (fieldValue == null) {
                    String msg = "属性：" + fieldName + " 不能为Null";
                    allmsg += msg + ",";
                    continue;
                }
            }

            boolean isAnnotationNotEmpty = field.isAnnotationPresent(NotEmptyString.class);
            if (isAnnotationNotEmpty) {
                if ("".equals(String.valueOf(fieldValue))) {
                    String msg = "属性：" + fieldName + " 不能为空";
                    allmsg += msg + ",";
                    continue;
                }
            }

            boolean isAnnotationNotZero = field.isAnnotationPresent(NotZero.class);
            if (isAnnotationNotZero) {
                double d = 0;
                try {
                    d = Double.parseDouble(fieldValue.toString());
                    if (d == 0) {
                        //判断大于0
                        String msg = "属性：" + fieldName + " 不能等于0";
                        allmsg += msg + ",";
                        continue;
                    }
                } catch (Exception e) {
                    if (e instanceof NumberFormatException) {
                        //标注为数字，其值不为数字
                        String msg = "属性：" + fieldName + " 标注为数字，其值不为数字";
                        allmsg += msg + ",";
                        continue;
                    }
                }
            }
        }
        if (!"".equals(allmsg.trim())) {
            LogUtil.Info(allmsg);
            throw new StatusException(StateMessage.LOSE_PARAM.appendMessage(allmsg));
        }
    }


    /**
     * 运行属性的getter方法，获取值
     *
     * @param field
     * @param instance
     * @return
     */
    default Object runGetter(Field field, Object instance) {
        // MZ: Find the correct method
        for (Method method : instance.getClass().getDeclaredMethods()) {
            if ((method.getName().startsWith("get")) && (method.getName().length() == (field.getName().length() + 3))) {
                if (method.getName().toLowerCase().endsWith(field.getName().toLowerCase())) {
                    // MZ: Method found, run it
                    try {
                        return method.invoke(instance);
                    } catch (IllegalAccessException | InvocationTargetException e) {
                        System.out.println("Could not determine method: " + method.getName());
                    }
                }
            }
        }
        return null;
    }
}
